3  Functions

So far, you have seen built-in functions of Python such as print(), len() & input() functions. Functions are like mini-programs that are meant to do a single task like the print() function prints the string on your terminal. Let’s break it down in simple terms to an 8th-grader child:

Imagine you have a box, and you can put different tasks or instructions inside that box. In programming, we call this box a “function.”

Now, why do we use functions? Well, think of it like having a set of instructions that you might need to use multiple times. Instead of writing those instructions again and again, you put them in a box (function). So, whenever you need to follow those instructions, you just use the box (call the function) instead of writing everything out each time.

# Define the function
1def add_numbers():
    result = 12 + 12
2    return result


# Print the result
3print("The sum is:", add_numbers())
1
def means define and is used to define a function in Python.
2
return In programming, the "return" keyword is like telling a function to give you something back. It’s a way for the function to share a specific result or piece of information with the rest of the program. Just as you put something in a box and then open the box to get it later, "return" allows a function to hand back its result when it’s done.
3
In this case add_numbers() function will return 24 and place it in the print function.

3.1 Functions with Arguments

When you pass the string to the print() function between parenthesis is called the arguments. Think of an argument as an extra set of powers a function can have. Let’s look at the following analogy: Imagine you have a Robot friend and you want to give it 2 numbers to add them and give you the result. Every time you give it different numbers and will give you the sum of it.

# Copy and Paste it into your shell window to see its output:

1def robot(num1 , num2):
    print(num1 + num2)

2robot(23, 42)
robot(3, 2)
robot(33, 12)
1
num1 & num2 are parameters (which is an empty Box)
2
23 & 42 are the arguments that will go inside the num1 & num2 parameters.

3.2 The None Value

In Python, we have a value called None which represents nothing or the absence of a value. The None keyword must be started with capital ‘N’. None is the only data type of the NoneType class. (will explain later what are classes)

def func_none(response):
    if response == 'Gold' or response == 'Silver':
        print('You Got Gold!!')
    elif response == 'Gold' or response == 'Silver':
        print('You Got Gold!!')
    else:
        return func_none

1result = func_none(response=input('Choose Gold or Silver, If you choose anything else you will get "None" '))

2print(result)
1
Here, the program will ask the user to enter the value and that value will be passed to the func_none() function as an argument and there the condition would be applied to the corresponding value if none of the conditions met, then the function returns the None value.
2
Here, if func_none() returns the result of the condition met like “Gold” or “Silver” then in the second line it would also print None and that’s because print() function on itself return nothing which is basically None.

3.3 Local and Global Scope

Let’s look at the code first and then we will see what is Local and Global.

1name = 'asad'

def func1():
    name = 'wafi'
2    print(name)

def func2():
    age = 18
3    print('Name: ',name)
    print('Age: ',age)

func1() 
func2() 
1
Here, the name variable can be accessed everywhere in the same program file e.g. func1.py while the name variable inside the func1() function can only be accessed inside that function.
2
Gives you wafi` in output.
3
Here the name variable will return the value ‘asad’ from the Global scope.

3.4 Global Statement

If you want to modify the Global variable inside a function you can use the global keyword before that variable but do not create a variable with the same name as the Global variable inside that function.

# Global variable
counter = 5

def increase_counter():
    global counter
    counter += 1

# Before calling the function
print("Before: Counter =", counter)

# Calling the function to modify the global variable
increase_counter()

# After calling the function
print("After: Counter =", counter)
Note

If you ever wanted to change the value of the Global variable you must use the global statement on the variable. So that Python knows which variable you are referring to.

3.5 Exception Handling

Getting an error or exception, in your Python programs means that the entire program will crash, But we don’t want this to happen in real-world scenarios, rather than you want the program to detect the error handle them and continue afterward.

For instance, run the following code in the editor:

def division(divider):
    print(42 / divider)

division(2) # Passing argument 2 to the function.
division(7)
1division(0)
division(1)
1
Here when you pass 0 dividers to the division() function means you are dividing 42 by 0 which will give you an error like the one below:

print(42 / divider)

1ZeroDivisionError: division by zero
1
ZeroDivisionError is an exception raised by an error of dividing the number by zero.

Now to prevent such types of exceptions & errors, Python has try and except clauses. The code that causes the error will go inside the try clause and the code that prevents the program from crashing goes into the except clause.

For example, Look at the above code with try and except clause:


def division(divider):
    try:
        print(42 / divider)
1    except ZeroDivisionError:
        print('You cannot divide with Zero')


2division(0)
1
ZeroDivisionError tells Python if you encounter ZeroDivisionError raise the following print message But you can write anything below except clause.
2
Now when you call the division function with 0 number it won’t crash the entire program but will give you a decent error message and the program will continue to execute other remaining instructions.

3.6 ZigZag Program for Fun:

Let’s use your previous knowledge of programming to create a small animation zigzag program. Type the following code into your file editor & save the file as zigzag.py:

2import time
import sys

try:
1    while True:
        counter = 4

        for i in range(10):

            if i <= 4:
                print(f'{counter*" "}********')
                counter -= 1
            elif counter == -1 or counter == 0:
                counter = 1
            else:
                print(f'{" "*counter}********')
                counter += 1

3            time.sleep(0.3)  # Adjust the delay time as needed to make it more like animation
except KeyboardInterrupt:
    sys.exit()
1
The while loop will never exit until you press Ctrl+c which calls the KeyboardInterrupt Error.
2
In Python, the time module helps you work with time-related functions. It’s like a toolbox with tools to handle time-related tasks.
3
Try to write a program without try and except which will cause the program to crash if you want to stop the while loop by Ctrl+c and will give you an ugly error message. However, for our program we it to cleanly handle KeyboardInterrupt in except clause then pressing Ctrl+c won’t crash the program rather it will stop smoothly as usual by calling the sys.exit() function.
Summary

Functions are the primary way to prevent you from duplicating your code. Functions are great to help you organize your code. You can think of it as the black boxes: they have input in the form of parameters and have output in the form of return values.

Back to top